package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.RedisConstant;
import com.itheima.dao.AddressDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Address;
import com.itheima.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.JedisPool;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = AddressService.class)
//涉及多表，使用事务管理
@Transactional
public class AddressServiceImpl implements AddressService {
    @Autowired
    private AddressDao addressDao;
     @Autowired
    private JedisPool jedisPool;

    @Override
    public void add(Address address) {
        addressDao.add(address);
        jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_Address,address.getImg());
    }



    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<Address> page = addressDao.selectByCondition(queryPageBean.getQueryString());
        List<Address> address = page.getResult();
        for (Address address1 : address) {
          String  img = "http://q6nhxtv5b.bkt.clouddn.com/"+address1.getImg();
          address1.setImg(img);
        }

        long total = page.getTotal();
        return  new PageResult(total,address);
    }


    @Override
    public Address findById(int id) {

        Address add = addressDao.findAddressById(id);
        add.setImg("http://q6nhxtv5b.bkt.clouddn.com/"+add.getImg());
        return  add;
    }

    @Override
    public void edit(Address address) {
        addressDao.edit(address);
    }

    @Override
    public void deleteById(int id) {
        addressDao.deleteByIds(id);
    }

    @Override
    public List<Address> findAll() {

        return addressDao.findAll();

    }

/*    private void addGroupAndItem(int id,Integer[] checkgroupIds){

        if (checkgroupIds!=null&&checkgroupIds.length>0){
           for (Integer checkgroupId : checkgroupIds) {
            Map<String, Integer> map = new HashMap<>();
            map.put("setmealId", id);
            map.put("groupId", checkgroupId);
               addressDao.addOrderAndAddress(map);
           }
        }
    }*/

}
